﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InstrumentCheck.Module;
using System.Data;
using InstrumentCheck.BLL;

namespace InstrumentCheck.WebUI.UserControl
{
    //public delegate void UCEventHandler(object sender, object eventArgs);
    
    public partial class UCSelectAssetNO : System.Web.UI.UserControl
    {
        //public event UCEventHandler OnVisibleChanged;
        //public AssetType type { get; set; }
        //public bool NeedShowBackground { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
                   
        }

        //public override bool Visible
        //{
        //    get
        //    {
        //        return base.Visible;
        //    }
        //    set
        //    {
        //        if (base.Visible == value)
        //            return;

        //        base.Visible = value;

        //        //...Set divFrame Style
        //        if(base.Visible)
        //            this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "ShowBack(" + divBack.ClientID + ")", true);     
        //        else
        //            this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "HideBack()", true);     

        //        if (OnVisibleChanged != null)
        //        {
        //            if (gvAssetNO.SelectedRow != null)
        //                OnVisibleChanged(this, gvAssetNO.SelectedRow);
        //            else
        //                OnVisibleChanged(this, "ABC");
        //        }
        //    }
        //}

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            string strTableName = txtQueryTableName.Text;
            List<Item> list = Asset_bll.GetQueryAssetInfo(strTableName, "ast_no", txtAssetNO.Text);
            Session["list"] = list;
            BindGridView(list);
        }

        private void BindGridView(List<Item> list)
        {            
            gvAssetNO.DataSource = list;
            gvAssetNO.DataBind();
            gvAssetNO.Visible = true;

            if (list == null)
                gvAssetNO.Visible = false;
        }

        protected void gvAssetNO_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Item item = e.Row.DataItem as Item;
                e.Row.Cells[1].Text = item.Keeper.DeptNO;
                e.Row.Cells[2].Text = item.Keeper.EmpChineseName;
                e.Row.Cells[7].Text = item.Keeper.EmpNO;

                string strValue = Decode(e.Row.Cells[0].Text) + "','" + Decode(item.Keeper.DeptNO) + "','";
                strValue += Decode(item.Keeper.EmpChineseName) + "','" + Decode(e.Row.Cells[3].Text) + "','";
                strValue += Decode(e.Row.Cells[4].Text) + "','" + Decode(e.Row.Cells[5].Text) + "','";
                strValue += Decode(e.Row.Cells[6].Text) + "','" + Decode(item.Keeper.EmpNO);

                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#E0E0E0'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                e.Row.Attributes["style"] = "Cursor:hand";
                e.Row.Attributes.Add("onclick", "returnValue('" + strValue + "')");
            }

            if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
                e.Row.Cells[7].Visible = false;
        }

        private string Decode(string str)
        {
            return System.Web.HttpUtility.HtmlDecode(str);
        }

        protected void gvAssetNO_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvAssetNO.PageIndex = e.NewPageIndex;
            List<Item> list = Session["list"] as List<Item>;
            BindGridView(list);
        }

    }          
    
}